********************************************************************************
* Replication file: "Creating crowny capitalists"
* June 24, 2019
* Contact: bernhard.reinsberg@glasgow.ac.uk
********************************************************************************

***********************************************************************  
* Table 2: BEEPS analysis
***********************************************************************

* Data

  use "Replication_BEEPS.dta", clear 
  
  rename time timeOLD
  gen timeID=timeOLD+1
  lab var timeID "One-period lagged variables - timeID"
  tab timeID
  drop if timeID<=7
  drop if incid==1
  sort iso3 timeID

  merge iso3 timeID using "BEEPS_20160728.dta"
  tab _merge
  sort cid timeID
  xtset cid timeID

  order cid iso3 timeID 
  g lngdppc=ln(gdppc_WDI)
  g lnpop=ln(unna_pop)
  g lnoilpc=ln(1+(1E+6)*ross_oil_prod/unna_pop)
  rename transparency HRV_index
  replace dy_colGBR=ceil(dy_colGBR)
   
  gen NonMiss=(br_inc!=.)
  egen N=sum(NonMiss), by(cid)
  drop NonMiss 

  set matsize 800
  cmp setup
  qui xi:reg br_inc i.cid i.timeID
  
  global X lngdppc p_polity2_QOG p_durable_QOG lnoilpc min_gdp_WDI
  global Firm age dom_gov for_prv inp_foreign0 inp_foreign
  global FirmEnd days_exp days_imp comp_pct 
  
  mat Mbr_inc=J(3,9,0)
 

* Analyses

  qui xtreg br_inc SCB QCB imf1 i.timeID if N>1, fe
  g samp=(e(sample))
  tab iso3 if samp
  
foreach y in br_inc {
  qui xtreg `y' SCB QCB imf1 i.timeID if N>1 & samp, fe cl(cid) 
  est store fe1`y'
  mat M`y'[1,1]=e(N)
  mat M`y'[2,1]=e(r2_w)
 
  qui xtreg `y' SCB QCB imf1 $X i.timeID if N>1 & samp, fe cl(cid) 
  est store fe2`y'
  mat M`y'[1,2]=e(N)
  mat M`y'[2,2]=e(r2_w)
 
  qui xtreg `y' SCB QCB imf1 $X $Firm i.timeID if N>1 & samp, fe cl(cid) 
  est store fe3`y'
  mat M`y'[1,3]=e(N)
  mat M`y'[2,3]=e(r2_w)
  }

foreach y in br_inc {
  di "Outcome: `y'"
  qui treatreg `y' SCB QCB imf1 i.cid i.timeID if N>1 & samp , treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag i.regid i.timeID) twostep hazard(imr)
  qui xtreg `y' SCB QCB imf1 imr _Icid* _ItimeID* if samp , fe cl(cid)
  est store he1`y'
  mat M`y'[1,4]=e(N)
  mat M`y'[2,4]=e(r2_w)
  qui cmp (`y'= SCB QCB imf1 imr _Icid* _ItimeID*) (SCB=lSCB _Itime*) (QCB=lQCB _Itime*) if N>1, indicators(samp $cmp_cont $cmp_cont) ro cl(cid)
  est store iv1`y'
  mat M`y'[1,7]=e(N1)
  mat M`y'[2,7]=M`y'[2,4]
  qui test ([SCB]lSCB [QCB]lQCB)
  mat M`y'[3,7]=r(chi2)
  drop imr
  
  qui treatreg `y' SCB QCB imf1 $X i.cid i.timeID if N>1 & samp , treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag $X i.regid i.timeID) twostep hazard(imr)
  qui xtreg `y' SCB QCB imf1 imr $X _Icid* _ItimeID* if samp , fe cl(cid)
  est store he2`y'
  mat M`y'[1,5]=e(N)
  mat M`y'[2,5]=e(r2_w)
  qui cmp (`y'= SCB QCB imf1 imr $X _Icid* _ItimeID*) (SCB=lSCB $X _Itime*) (QCB=lQCB $X _Itime*) if N>1, indicators(samp $cmp_cont $cmp_cont) ro cl(cid)
  est store iv2`y'
  mat M`y'[1,8]=e(N1)
  mat M`y'[2,8]=M`y'[2,5] 
  qui test ([SCB]lSCB [QCB]lQCB)
  mat M`y'[3,8]=r(chi2)
  drop imr
  
  qui treatreg `y' SCB QCB imf1 $X $Firm i.cid i.timeID if N>1 & samp , treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag $X $Firm i.regid i.timeID) twostep hazard(imr)
  qui xtreg `y' SCB QCB imf1 imr _Icid* $X $Firm _ItimeID* if samp , fe cl(cid)
  est store he3`y'
  mat M`y'[1,6]=e(N)
  mat M`y'[2,6]=e(r2_w)  
  qui cmp (`y'= SCB QCB imf1 imr $X $Firm _Icid* _ItimeID*) (SCB=lSCB $X $Firm _Itime*) (QCB=lQCB $X $Firm _Itime*) if N>1, indicators(samp $cmp_cont $cmp_cont) ro cl(cid)
  est store iv3`y'
  mat M`y'[1,9]=e(N1)
  mat M`y'[2,9]=M`y'[2,6] 
  qui test ([SCB]lSCB [QCB]lQCB)
  mat M`y'[3,9]=r(chi2)
  drop imr
  }

  ** Table 3 **
  foreach y in br_inc{
  esttab fe*`y' he*`y' iv*`y', cells(b(star fmt(3)) se(par fmt(3))) keep(imf1 *SC* *QC* $X imr $Firm) order(*SC* *QC* imf1 $X imr $Firm imr)   
  di "Diagnostics:"
  mat li M`y'
  }
  